import {
    Component,
    ViewChild,
    Injector,
    Output,
    EventEmitter,
} from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap';
import {
    PipelineServiceProxy,
    PrintPipelineListDto,
    DepartmentServiceProxy,
    PrintServiceProxy,
    NameValueDtoOfInt32,
    ComboboxItemDto,
    PrintCommonDto,
    PrintJJDDto,
    JJDList,
    GetDeliveryRecordListDto,
    BatchTaskServiceProxy,
} from '@shared/service-proxies/service-proxies';
import { AppComponentBase } from '@shared/common/app-component-base';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';


@Component({
    selector: 'batchPrintJjdModal',
    templateUrl: './batch-print-jjd-modal.component.html',
})
export class BatchPrintjjdModalComponent extends AppComponentBase {
    @ViewChild('actionModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator') paginator: Paginator;

    active = false;
    saving = false;
    departments = new Array<NameValueDtoOfInt32>();
    oldprint = '';
    teamGroups: ComboboxItemDto[] = [];
    teamGroupName: ComboboxItemDto;
    batchId:Array<number>;
    printDeliveryRecord: GetDeliveryRecordListDto[];
    remark = '';
    printInputs: PrintCommonDto[];
    printInput = new PrintCommonDto();
    jJDList = new Array<JJDList>();
    jJD = new JJDList();
    i = 0;
    sum = 0;
    constructor(
        injector: Injector,
        private _batchService: BatchTaskServiceProxy,
        private _printServiceProxy: PrintServiceProxy
    ) {
        super(injector);
    }

    show(batchId: Array<number>): void {
        this.batchId = batchId;
        //console.log(pipe);
        this.active = true;
        this.primengTableHelper.showLoadingIndicator();
        // this._departmentServiceProxy.getMesDepartments().subscribe((result) => {
        //     this.departments = result;
        // });
        this._printServiceProxy.getPrintDrow().subscribe((result) => {
            // this.teamGroupName = result.groupName;
            this.teamGroups = result;
            this._printServiceProxy.getUserPrint().subscribe((result1) => {
                if (result1 != null) {
                    this.oldprint = result1.value;
                }
                this.teamGroupName = result1;
            });
        });
        this._batchService
            .getProduceBatchById(this.batchId)
            .subscribe((result) => {
                this.printDeliveryRecord = result;
                this.primengTableHelper.hideLoadingIndicator();
                this.modal.show();
            });
    }

    save(): void {
        this.primengTableHelper.showLoadingIndicator();
        console.log(this.teamGroupName);
        if (this.teamGroupName.value != '') {
            if (this.oldprint != this.teamGroupName.value) {
                this._printServiceProxy
                    .createOrUpdateUserPrint(this.teamGroupName.value)
                    .subscribe((result) => {});
            }
        }
        this.printInputs = new Array<PrintCommonDto>();
        this.jJDList = new Array<JJDList>();
        this.printDeliveryRecord.forEach((item) => {
            this.printInput = new PrintCommonDto();
            this.printInput.printJJDDto = new PrintJJDDto();
            this.jJD = new JJDList();

            this.printInput.printSet = this.teamGroupName.value;
            this.printInput.type = 'JiaoJieDan';
            this.printInput.printJJDDto.xxCheJian = '55车间';
            this.printInput.printJJDDto.jsCheJian =
                item.destinationId.toString() + '车间';
            this.printInput.printJJDDto.jjdid = item.id.toString();
            this.jJD.productAmount = item.deliveredQuantity;
            this.jJD.barCode = item.certificateId;
            this.jJD.remark = item.remark;
            this.jJD.drawingNum = item.drawingCode;
            this.jJD.productName = item.drawingName;
            this.jJD.taskNum = item.taskCode;
            this.jJDList.push(this.jJD);
        });
        this.printInput.printJJDDto.jjdList = this.jJDList;
        this.print(this.printInput);
    }
    print(data: PrintCommonDto) {
            this._printServiceProxy
            .print(data)
            .subscribe((result) => {
                this.primengTableHelper.hideLoadingIndicator();
                if (result === '打印成功') {
                    this.notify.info(result);
                } else {
                    this.notify.error(result);
                }
            });
    }
    onRowEditInit(data) {
        console.log(data);
        let pipe = new Array<GetDeliveryRecordListDto>();
        for (let index = 0; index < this.printDeliveryRecord.length; index++) {
            //let nodeWorker = new GetNodeWorkOutput();
            if (index !== data) {
                pipe.push(this.printDeliveryRecord[index]);
            }
        }
        this.printDeliveryRecord = pipe;
        console.log(this.printDeliveryRecord);
    }
    close(): void {
        this.saving = false;
        this.modal.hide();
    }
}
